package jeuDeLaVie;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MaVue extends JFrame implements Observer{


        private static final long serialVersionUID = 7779787387700741530L;
        
        private static final int TAILLE_X_VUE = 40;
        private static final int TAILLE_Y_VUE = 40;

                // référence sur l'environnement
        Environnement monde;
        JPanel[][] tabCarteV;
        
        
        
        
        // Constructeur
        public MaVue(Environnement e){
            super();
            monde = e;
            init();
        }
        
        private void init(){
            this.setTitle("Jeu de la Vie");
            this.setSize(600, 500);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            // Création des JPanel
            JPanel principal = new JPanel(new BorderLayout());
            JPanel options = new JPanel(new FlowLayout());
            JPanel carteV = new JPanel(new GridLayout(TAILLE_X_VUE,TAILLE_Y_VUE,2,2));

            // Placement des JPanel sur le principal
            principal.add(carteV, BorderLayout.CENTER);
            principal.add(options, BorderLayout.NORTH);

            // Création et placement des boutons d'options
            
            JButton bStart1 = new JButton("SIMPLE");
            JButton bStarts = new JButton("MULTI");
            JButton bStartf = new JButton("FIN");
            JButton bReset1 = new JButton("1VIE");
            JButton bResets = new JButton("4VIE");
            JButton bVille = new JButton("VILLE");
            JButton bStartV = new JButton("AVANCE");
            
            
            options.add(bReset1);
            options.add(bResets);
            options.add(bStart1);
            options.add(bStarts);
            options.add(bStartV);            
            options.add(bReset1);
            options.add(bResets);
            options.add(bVille);
            options.add(bStartf);
            
            bStart1.setBackground(Color.BLUE);
            bStarts.setBackground(Color.GREEN);
            bStartf.setBackground(Color.RED);
            bReset1.setBackground(Color.BLUE);
            bResets.setBackground(Color.GREEN);
            bVille.setBackground(Color.GRAY);
            bStartV.setBackground(Color.YELLOW);
            
            
            
            
            // Création et placement des JPanel de la carte
            tabCarteV = new JPanel[TAILLE_X_VUE][TAILLE_Y_VUE];
            for(int i=0; i<TAILLE_X_VUE; i++){
                    for(int j=0; j<TAILLE_Y_VUE; j++){
                            tabCarteV[i][j] = new JPanel();
                            
                            tabCarteV[i][j].addMouseListener(new MouseListenerIJ(i, j));
                            
                            
                            
                            
                            switch (monde.getEtatCellule(i, j)) {
                            case 0:  tabCarteV[i][j].setBackground(Color.WHITE);     break;
                            case 1:  tabCarteV[i][j].setBackground(Color.GREEN);     break;
                            case 2:  tabCarteV[i][j].setBackground(Color.YELLOW);	 break;
                            case 3:  tabCarteV[i][j].setBackground(Color.RED);       break;
                            case 4:  tabCarteV[i][j].setBackground(Color.BLUE);      break;
                            case 5:  tabCarteV[i][j].setBackground(Color.GRAY);      break;
                            case 6:  tabCarteV[i][j].setBackground(Color.ORANGE);	 break;
                            case 7:  tabCarteV[i][j].setBackground(Color.MAGENTA);   break;
                            case 8:  tabCarteV[i][j].setBackground(Color.CYAN);      break;
                            default: tabCarteV[i][j].setBackground(Color.BLACK); 	 break;
                        }
                            /*tabCarteV[i][j].setBackground(Color.BLACK);*/
                            carteV.add(tabCarteV[i][j]);
                    }
            }
         
            // Définir le JPanel principal
            this.setContentPane(principal);
            this.setVisible(true);


             bStart1.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.run1(monde);
                	 	
                         
                 }
         }); 

 
             bStarts.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.runs(monde);
                	 	
                         
                 }
         }); 
          
             
             bStartf.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.runf(monde);
                	 	
                         
                 }
         });
          
             
             bReset1.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.generer1();
                	 	
                         
                 }
         });
 
             bResets.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.generers();
                	 	
                         
                 }
         });
             
             bVille.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.genererv();
                	 	
                         
                 }
         });
             
             bStartV.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent evenement){
                         //il faudra lancer un nouveau thread
                	 	
                	 		monde.rune(monde);
                	 	
                         
                 }
         });
             
        }
        
        
        
        
        
        
        public void update(){
                for(int i=0; i<TAILLE_X_VUE; i++){
                        for(int j=0; j<TAILLE_Y_VUE; j++){
                               /* if (monde.getEtatCellule(i, j) == 0){
                                        tabCarteV[i][j].setBackground(Color.WHITE);
                                }
                                else {
                                        tabCarteV[i][j].setBackground(Color.RED);
                                }*/
                        	
                        	switch (monde.getEtatCellule(i, j)) {
                            case 0:  tabCarteV[i][j].setBackground(Color.WHITE);     break;
                            case 1:  tabCarteV[i][j].setBackground(Color.GREEN);     break;
                            case 2:  tabCarteV[i][j].setBackground(Color.YELLOW);	 break;
                            case 3:  tabCarteV[i][j].setBackground(Color.RED);      break;
                            case 4:  tabCarteV[i][j].setBackground(Color.BLUE);      break;
                            case 5:  tabCarteV[i][j].setBackground(Color.GRAY);      break;
                            case 6:  tabCarteV[i][j].setBackground(Color.ORANGE);	 break;
                            case 7:  tabCarteV[i][j].setBackground(Color.MAGENTA);   break;
                            case 8:  tabCarteV[i][j].setBackground(Color.CYAN);      break;
                            default: tabCarteV[i][j].setBackground(Color.BLACK); 	 break;
                        }
                        	
                        	
                        }
                }
        }
        
        

        
        private class MouseListenerIJ extends MouseAdapter {
        	private int i,j;
        	
        	public MouseListenerIJ(int _i, int _j) {
        		i = _i;
        		j = _j;
        	}
        	
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		// TODO Auto-generated method stub
        		super.mouseClicked(e);
        		monde.cree(i,j);
        	}
        	
        	@Override
			public void mouseEntered(MouseEvent arg0) {

				tabCarteV[i][j].setBackground(Color.BLACK);
			}

			@Override
			public void mouseExited(MouseEvent arg0) {

	
                            switch (monde.getEtatCellule(i, j)) {
                            case 0:  tabCarteV[i][j].setBackground(Color.WHITE);     break;
                            case 1:  tabCarteV[i][j].setBackground(Color.GREEN);     break;
                            case 2:  tabCarteV[i][j].setBackground(Color.YELLOW);	 break;
                            case 3:  tabCarteV[i][j].setBackground(Color.RED);       break;
                            case 4:  tabCarteV[i][j].setBackground(Color.BLUE);      break;
                            case 5:  tabCarteV[i][j].setBackground(Color.GRAY);      break;
                            case 6:  tabCarteV[i][j].setBackground(Color.ORANGE);	 break;
                            case 7:  tabCarteV[i][j].setBackground(Color.MAGENTA);   break;
                            case 8:  tabCarteV[i][j].setBackground(Color.CYAN);      break;
                            default: tabCarteV[i][j].setBackground(Color.BLACK); 	 break;


				}
			}
        	
        	
        }
        
        

}



